//
//  ResetPhoneViewController.m
//  ShrimpCrowdordering
//
//  Created by 骆锴 on 2018/8/27.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ResetPhoneViewController.h"

@interface ResetPhoneViewController ()<UITextFieldDelegate>

@property (nonatomic,assign) NSInteger verCodeTime;
@property (nonatomic,strong) NSTimer *verCodeTimer;//验证码计时器
@property (nonatomic,strong) UIButton *verCodeBtn;//发送验证码按钮
@property (nonatomic, strong) UITextField *PhoneNumber;
@property (nonatomic, strong) UITextField *code;
@property (nonatomic, strong) NSMutableString *phone;

@end

@implementation ResetPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _phone = [NSMutableString string];
    self.view.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    self.title = @"更换手机号";
    
    [self setUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUI {
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(96))];
    baseView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:baseView];
    
    UILabel *titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(16), ScreenX375(60), ScreenX375(15))];
    titleLabel.text = @"手机号";
    titleLabel.font = Kfont(15);
    titleLabel.textColor = REXADECIMALCOLOR(@"#333333");
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [baseView addSubview:titleLabel];
    
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
    UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
    [bar setItems:buttonsArray];
    
    _PhoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(100), ScreenX375(16), ScreenX375(150), ScreenX375(15))];
    _PhoneNumber.placeholder = @"请输入新手机号";
    _PhoneNumber.delegate = self;
    _PhoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    _PhoneNumber.inputAccessoryView = bar;
    [_PhoneNumber addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [baseView addSubview:_PhoneNumber];
    
    _verCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(279), ScreenX375(13), ScreenX375(80), ScreenX375(24))];
    _verCodeBtn.backgroundColor = REXADECIMALCOLOR(@"#cccccc");
    _verCodeBtn.layer.cornerRadius = ScreenX375(12);
    _verCodeBtn.clipsToBounds = YES;
    
    [_verCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_verCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _verCodeBtn.titleLabel.font = Kfont(12);
    _verCodeBtn.enabled = NO;
    [_verCodeBtn addTarget:self action:@selector(sendVerCode:) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:_verCodeBtn];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(47.5) - 0.5, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = LineColor;
    [baseView addSubview:line];
    
    UILabel *titleLabel2 =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(64), ScreenX375(60), ScreenX375(15))];
    titleLabel2.text = @"验证码";
    titleLabel2.font = Kfont(15);
    titleLabel2.textColor = REXADECIMALCOLOR(@"#333333");
    titleLabel2.textAlignment = NSTextAlignmentLeft;
    [baseView addSubview:titleLabel2];
    
    UIToolbar *bar2 = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
    UIBarButtonItem * button3 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * button4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton1 = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
    NSArray * buttonsArray2 = [NSArray arrayWithObjects:button3,button4,doneButton1,nil];
    [bar2 setItems:buttonsArray2];
    
    _code = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(100), ScreenX375(64), ScreenX375(150), ScreenX375(15))];
    _code.placeholder = @"请输入验证码";
    _code.keyboardType = UIKeyboardTypeNumberPad;
    _code.inputAccessoryView = bar2;
    [baseView addSubview:_code];
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT-ScreenX375(49)-NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(49))];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sentChange) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.titleLabel.font = Kfont(18);
    sureBtn.clipsToBounds = YES;
    [self.view addSubview:sureBtn];
}

//发送验证码
- (void)sendVerCode:(UIButton *)sender{
    _phone = [_PhoneNumber.text mutableCopy];
    _verCodeBtn.enabled = NO;
    [_verCodeBtn setBackgroundImage:nil forState:UIControlStateNormal];
    self.verCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCountAction) userInfo:nil repeats:YES];
    self.verCodeTime = 60;
    [self.verCodeTimer fire];
    [self.verCodeBtn setTitleColor:FONTCOLOR_LIGHTGRAY forState:UIControlStateNormal];
    self.verCodeBtn.backgroundColor = REXADECIMALCOLOR(@"#dddddd");
    self.verCodeBtn.enabled = NO;

    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/sendSms",REQUESTHEADER] andParameter:@{@"phone":_PhoneNumber.text} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            if (successResponse[@"data"][@"code"]) {
                NSLog(@"%@",successResponse);
                [HttpManager showNoteMsg:@"验证码已发送"];
            }

        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)timerCountAction{
    self.verCodeTime--;
    
    [self.verCodeBtn setTitle:[NSString stringWithFormat:@"%ld秒后重发",(long)self.verCodeTime] forState:UIControlStateNormal];
    if (self.verCodeTime == 0) {
        [self.verCodeBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
        [self.verCodeBtn setBackgroundImage:ImageWithName(@"按钮背景") forState:UIControlStateNormal];
        self.verCodeBtn.enabled = YES;
        [self.verCodeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self.verCodeTimer invalidate];
        self.verCodeTimer = nil;
    }
}

- (void) textFieldDidChange:(id) sender {
    UITextField *textField = (UITextField *)sender;
    if (textField.text.length == 11) {
        _verCodeBtn.enabled = YES;
        [_verCodeBtn setBackgroundImage:ImageWithName(@"按钮背景") forState:UIControlStateNormal];
    }else{
        _verCodeBtn.enabled = NO;
        [_verCodeBtn setBackgroundImage:nil forState:UIControlStateNormal];
    }
    
}
-(void)sentChange{
    if (_phone.length == 0) {
        [HttpManager showNoteMsg:@"请输入手机号码"];
    }else{
        NSDictionary *parameter = @{@"phone":_phone,@"code":_code.text};
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/checkSms",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
            NSString *code = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
            if ([code isEqualToString:@"200"]) {
                [self sentValue];
            }else {
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    }
}

-(void)sentValue{
    NSDictionary *parameter = @{@"id":kNSUDefaultReadKey(@"userId"),@"phone":_phone};
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/updateUserInfo",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
            NSString *code = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
            if ([code isEqualToString:@"200"]) {
                [HttpManager showNoteMsg:@"修改成功"];
            }else {
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
}

-(void)resignKey{
    [_PhoneNumber resignFirstResponder];
    [_code resignFirstResponder];
}
@end
